﻿// ———————————————————————–
// <copyright file="ValListHelper.cs" company="The MITRE Corporation">
//    Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// ValListHelper.cs - Helper class for using ValLists
// Project: EDXLSharp_AWSRouter - ICNETSerializers
//
// Language:    C#, .NET 4.0
// Platform:    Windows 7, VS 2013
// Author:      Brian Wilkins The MITRE Corporation
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)

using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace ICNETSerializers
{
  public static class ValListsHelper
  {
    /// <summary>
    /// Serialize a ValLists object to an XElement
    /// </summary>
    /// <param name="TheValLists">ValLists to serialize</param>
    /// <returns>XElement representing ValLists</returns>
    public static string ToString(ValLists TheValLists)
    {
      if (TheValLists != null)
      {
        XmlSerializer xs = new XmlSerializer(typeof(ValLists));

        using (StringWriter sw = new StringWriter())
        {
          xs.Serialize(sw, TheValLists);

          return sw.ToString();
        }
      }
      else
      {
        return "";
      }
    }

    /// <summary>
    /// Deserialize string into ValLists object
    /// </summary>
    /// <param name="RulesString">string to deserialize</param>
    /// <returns>ValLists</returns>
    public static ValLists FromString(string RulesString)
    {
      XmlSerializer serializer = new XmlSerializer(typeof(ValLists));
      ValLists result;

      using (TextReader reader = new StringReader(RulesString))
      {
        result = (ValLists)serializer.Deserialize(reader);
      }

      return result;
    }
    /// <summary>
    /// Serialize a ValLists object to an XElement
    /// </summary>
    /// <param name="TheValLists">ValLists to serialize</param>
    /// <returns>XElement representing ValLists</returns>
    public static XElement ToXElement(ValLists TheValLists)
    {
      if (TheValLists != null)
      {
        XmlSerializer xs = new XmlSerializer(typeof(ValLists));
        XDocument d = new XDocument();
        using (XmlWriter xw = d.CreateWriter())
        {
          xs.Serialize(xw, TheValLists);
        }
        return d.Root;
      }
      else
      {
        return null;
      }
    }

    /// <summary>
    /// Deserialize XElement into ValLists object
    /// </summary>
    /// <param name="RulesElement">XElement to deserialize</param>
    /// <returns>ValLists</returns>
    public static ValLists FromXElement(XElement RulesElement)
    {
      XmlSerializer serializer = new XmlSerializer(typeof(ValLists));
      return (ValLists)serializer.Deserialize(RulesElement.CreateReader());
    }
  }
}